package com.lagou.boot;

import com.lagou.coder.RpcDecoder;
import com.lagou.coder.RpcEncoder;
import com.lagou.handler.RpcRequestHandler;
import com.lagou.handler.UserServiceHandler;
import com.lagou.protocol.RpcRequest;
import com.lagou.ser.JSONSerializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.AttributeKey;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.CreateMode;

public class ServerBoot {

    private static final String PATH = "/NETTY_IPS";

//    public static void main(String[] args) throws InterruptedException {
//        //启动服务器
//        UserServiceImpl.startServer("127.0.0.1",8999);
//    }

    //创建一个方法启动服务器
    public static void startServer(String ip , int port) throws Exception {
        //1.创建两个线程池对象
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup  workGroup = new NioEventLoopGroup();

        //2.创建服务端的启动引导对象
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        //3.配置启动引导对象
        serverBootstrap.group(bossGroup,workGroup)
                //设置通道为NIO
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .option(ChannelOption.TCP_NODELAY, Boolean.TRUE)
                //创建监听channel
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        //获取管道对象
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();



                        //给管道对象pipeLine 设置编码
                        pipeline.addLast(new StringEncoder());
//                        pipeline.addLast(new StringDecoder());
//                        pipeline.addLast(new RpcEncoder(RpcRequest.class, new JSONSerializer()));
                        pipeline.addLast(new RpcDecoder(RpcRequest.class, new JSONSerializer()));
                        //把我们自顶一个ChannelHander添加到通道中
//                        pipeline.addLast(new UserServiceHandler());
                        // 这里我们新增rpcRequest的handler
                        pipeline.addLast(new RpcRequestHandler());
                    }
                });

        //4.绑定端口
        serverBootstrap.bind(port).sync();
        bindZk(ip, port);
    }

    private static void bindZk(String ip, int port) throws Exception {
        CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new RetryNTimes(3, 1000));
        client.start();
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(PATH+"/"+ip+":"+port, (ip+":"+port).getBytes());
    }

}
